<?php
// 分类模型
class CategoryAction extends CommonAction {
	//赋值可用模型
	public function _before_index() {
		$this->assign('classmodule',D('Category')->getModule());
	}
	//赋值可用模型
	public function _before_add() {
		$this->assign('classmodule',D('Category')->getModule());
	}
	//赋值可用模型
	public function _before_edit() {
		$this->assign('classmodule',D('Category')->getModule());
	}
	//添加分类
	public function insert(){
		$model = D ('Category');
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}		
		if (($classid = $model->add ())!==false) { //保存成功
			D('Category')->setParentsChilds($classid); //更新指定目录及其一条线上的所有栏目的pid,pids,child,childids字段
			if($_POST['classrewrite']){
				$data['rewrite']=$_POST['classrewrite'];
				$data['url']=strtolower($_POST['classmodule']).'/index/id/'.$classid;
				M('Router')->add($data);
			}
			$this->success ('新增成功!');
		} else {
			//失败提示
			$this->error ('新增失败!');
		}
	}
	//编辑分类
	public function update() {
		$model = D ( 'Category' );
		$category = $model->find($_POST['classid']);		
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		// 更新数据
		if ($model->save () !== false) {
			D('Category')->setParentsChilds($_POST['classid']); //更新指定目录及其一条线上的所有栏目的pid,pids,child,childids字段
			//路由的保存			
			$data['url']=strtolower($_POST['classmodule'])."/index/id/".$_POST['classid'];
			if($_POST['classrewrite']){
				$data['rewrite']=$_POST['classrewrite'];
				if(M('Router')->where("url='{$data['url']}'")->count()){
					M('Router')->where("url='{$data['url']}'")->save($data);
				}else{
					M('Router')->add($data);
				}
			}else{
				M('Router')->where("url='{$data['url']}'")->delete();
			}
			$this->success ('编辑成功!');
		} else {
			//错误提示
			$this->error ('编辑失败!');
		}
	}	
	
	//删除分类的同时，删除路由规则以及子栏目以及其模型下对应的文章等等
	public function _before_foreverdelete() {
		if($_REQUEST['classid']){
			$classid = is_array($_REQUEST['classid']) ? implode(',',$_REQUEST['classid']) :(preg_match('/^\d+(,\d+)*$/',$_REQUEST['classid']) || is_numeric($_REQUEST['classid']) ? $_REQUEST['classid'] : 0);
			if($classid){
				//路由
				$list = D('Category')->where("classid in ($classid)")->field('classrewrite,classmodule')->select();
				foreach($list as $rs){
					if($rs['classrewrite']!='') $rewrite .= ($rewrite!='' ? ',' : '') . "'{$rs['rewrite']}'";	
					M($rs['classmodule'])->where("tid='$classid'")->delete();
				} 		
				if($rewrite) M('Router')->where('rewrite in ('.$rewrite.')')->delete();	

				//子栏目
				D('Category')->where("classpid in ($classid)")->delete();
			}
		}
	}
	
	//彻底删除
	public function foreverdelete() {
		$model = D ('Category');
		$pk = $model->getPk ();
		$id = $_REQUEST [$pk];
		if ( preg_match('/^\d+(,\d+)*$/',$id) ) {
			$list = D('Category')->where("classid in ($id)")->field('classpid')->select();			
			$condition = array ($pk => array ('in', $id ) );
			if (false !== $model->where ( $condition )->delete ()) {
				foreach($list as $rs){
					if($rs['classpid']){
						D('Category')->setParentsChilds($rs['classpid']); //更新指定目录及其一条线上的所有栏目的pid,pids,child,childids字段						
					}
				} 	
				$this->success ('删除成功！');
			} else {
				$this->error ('删除失败！');
			}
		} else {
			$this->error ( '非法操作' );
		}		
		$this->forward ();
	}
}